package com.coffee.soft.controller;

import com.alibaba.fastjson.JSON;
import com.coffee.soft.entity.ItemsCarousel;
import com.coffee.soft.entity.ItemsDetail;
import com.coffee.soft.entity.UsersItems;
import com.coffee.soft.service.ItemsManangerService;
import com.sun.org.apache.xpath.internal.SourceTree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @Author:LiuRuidong
 * @Description:  商品后台管理
 * @Date: Created in 23:45 2018/2/5
 * @Modified By:
 */
@Controller
public class ItemsManagerController {
    @Autowired
    private ItemsManangerService itemsManangerService;
   /* *//**
     * @Author：LiuRuidong
     * @Description: 选择新的产品时要更新他的新品状态
     * @Date:2018/2/5 23:52
     * @Param:
     * @return 1:表示成功，0表示失败
     *//*
    @RequestMapping("/chooseNew")
    @ResponseBody
    public void  chooseNewImageHandler(HttpServletRequest request, HttpServletResponse response,String itemsId) {
        String callback = request.getParameter("callback");
        int result  =itemsManangerService.chooseNew(itemsId);
        String json = JSON.toJSONString(result);
        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset=utf-8");
            PrintWriter pw = response.getWriter();
            if ((callback!="")&&(callback!=null)){
                json=callback+"("+json+")";
            }
            pw.println(json);
            pw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
*//**
 * @Author：LiuRuidong
 * @Description: 选择轮播图时更新其现有的状态
 * @Date:2018/2/7 10:46
 * @Param:
 * @return
 *//*
    @RequestMapping("/chooseCarousel")
    @ResponseBody
    public void  chooseCarouselImageHandler(HttpServletRequest request, HttpServletResponse response,String itemsId) {
        String callback = request.getParameter("callback");
        int result  =itemsManangerService.chooseCarousel(itemsId);
        String json = JSON.toJSONString(result);
        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset=utf-8");
            PrintWriter pw = response.getWriter();
            if ((callback!="")&&(callback!=null)){
                json=callback+"("+json+")";
            }
            pw.println(json);
            pw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
 }*/
 /**
 * @Author：LiuRuidong
 * @Description: 选择非新的商品
 * 测试已成功
 * @Date:2018/2/5 23:57
 * @Param:
 * @return
 */
    @RequestMapping("/chooseNotNew")
    @ResponseBody
    public void  chooseNotNewImageHandler(HttpServletRequest request, HttpServletResponse response,String itemsId) {
        String callback = request.getParameter("callback");
        itemsManangerService.chooseNew(itemsId);
        List<ItemsCarousel> itemsNotNew =itemsManangerService.selectNotNew();
        String json = JSON.toJSONString(itemsNotNew);
        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset=utf-8");
            PrintWriter pw = response.getWriter();
            if ((callback!="")&&(callback!=null)){
                json=callback+"("+json+")";
            }
            pw.println(json);
            pw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
/**
 * @Author：LiuRuidong
 * @Description: 点击加的时候，选择未轮播
 * @Date:2018/2/5 23:59
 * @Param:
 * @return
 */
    @RequestMapping("/chooseNotCarousel")
    @ResponseBody
    public void  chooseNotCarouselHandler(HttpServletRequest request, HttpServletResponse response,String itemsId) {
        String callback = request.getParameter("callback");
        int i = itemsManangerService.chooseCarousel(itemsId);
        List<ItemsCarousel> itemsCarousels  =itemsManangerService.selectNotCarousel();
        String json = JSON.toJSONString(itemsCarousels);
        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset=utf-8");
            PrintWriter pw = response.getWriter();
            if ((callback!="")&&(callback!=null)){
                json=callback+"("+json+")";
            }
            pw.println(json);
            pw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * @Author：LiuRuidong
     * @Description: 确认轮播图片
     * @Date:2018/2/6 0:23
     * @Param:
     * @return
     */
    @RequestMapping("/confirmCarousel")
    @ResponseBody
    public void confirmCarouselHandler(HttpServletRequest request, HttpServletResponse response,String itemsId) {
        String callback = request.getParameter("callback");
        int result =itemsManangerService.confirmCarousel(itemsId);
        String json = JSON.toJSONString(result);
        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset=utf-8");
            PrintWriter pw = response.getWriter();
            if ((callback!="")&&(callback!=null)){
                json=callback+"("+json+")";
            }
            pw.println(json);
            pw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * @Author：LiuRuidong
     * @Description: 确认新品
     * @Date:2018/2/6 0:24
     * @Param:
     * @return
     */
    @RequestMapping("/confirmNew")
    @ResponseBody
    public int confirmNewHandler(String itemsId) {
        int result =itemsManangerService.confirmNew(itemsId);
        return result;
    }
}


